﻿using FaceWebServer.DB.UI;
using FaceWebServer.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FaceWebServer.Service
{
    public class SystemMenuService : BaseService, ISystemMenuService
    {
        public SystemMenuService(DbContext context) : base(context)
        {
        }



        public MenusInfoResultDTO GetSystemMenu()
        {
            List<SystemMenuEntity> systemMenuEntities = new List<SystemMenuEntity>();

            systemMenuEntities=this.Query<SystemMenuEntity>(s => s.id > 0).ToList();


            SystemMenu rootNode = new SystemMenu()
            {
                Id = 0,
                Icon = "",
                Href = "",
                Title = "根目录",
            };

            GetTreeNodeListByNoLockedDTOArray(systemMenuEntities.ToArray(), rootNode);

            MenusInfoResultDTO menusInfoResultDTO = new MenusInfoResultDTO();
            menusInfoResultDTO.MenuInfo = rootNode.Child;
            menusInfoResultDTO.LogoInfo = new LogoInfo();
            menusInfoResultDTO.HomeInfo = new HomeInfo();

            return menusInfoResultDTO;
        }

        /// <summary>
        /// 递归处理数据
        /// </summary>
        /// <param name="systemMenuEntities"></param>
        /// <param name="rootNode"></param>
        public static void GetTreeNodeListByNoLockedDTOArray(SystemMenuEntity[] systemMenuEntities, SystemMenu rootNode)
        {
            if (systemMenuEntities == null || systemMenuEntities.Count() <= 0)
            {
                return;
            }

            var childreDataList = systemMenuEntities.Where(p => p.pid == rootNode.Id);
            if (childreDataList != null && childreDataList.Count() > 0)
            {
                rootNode.Child = new List<SystemMenu>();

                foreach (var item in childreDataList)
                {
                    SystemMenu treeNode = new SystemMenu()
                    {
                        Id = item.id,
                        Icon = item.icon,
                        Href = item.href,
                        Title = item.title,
                    };
                    rootNode.Child.Add(treeNode);
                }

                foreach (var item in rootNode.Child)
                {
                    GetTreeNodeListByNoLockedDTOArray(systemMenuEntities, item);
                }
            }
        }
    }
}
